package com.m3839.sdk.pay;

import android.app.Activity;
import android.text.TextUtils;

import com.m3839.sdk.common.core.ErrorConstant;
import com.m3839.sdk.common.util.ConditionUtils;
import com.m3839.sdk.common.util.LogUtils;
import com.m3839.sdk.login.core.InitManager;
import com.m3839.sdk.login.core.LoginManager;
import com.m3839.sdk.login.core.UserStorage;
import com.m3839.sdk.pay.bean.PayInfo;
import com.m3839.sdk.pay.constant.PayConstant;
import com.m3839.sdk.pay.listener.HykbPayListener;
import com.m3839.sdk.pay.model.IPayModel;
import com.m3839.sdk.pay.model.PayApiModel;
import com.m3839.sdk.pay.task.PayTask;

import java.util.HashMap;
import java.util.Map;

/**
 * Description:HykbPayManager
 *
 * @author 陈宝阳
 * @create 2020/8/13 11:29
 */
public class PayManager {

  private static final String TAG = "Pay";

  private final Map<PayInfo, PayTask> payTaskMap = new HashMap<>();

  /** 支付模块涉及的数据源 */
  private final IPayModel payModel;

  private PayManager() {
    payModel = new PayApiModel();
  }

  /**
   * 返回HykbPayManager对象
   */
  public static PayManager getInstance() {
    return SingletonHolder.INSTANCE;
  }

  private static class SingletonHolder {
    private static final PayManager INSTANCE = new PayManager();
  }

  public void doPay(final Activity activity, final PayInfo payInfo, HykbPayListener listener) {
    LogUtils.i(TAG, "doPay");

    if(!ConditionUtils.commonCondition(activity, new ConditionUtils.ConditionCallback() {
      @Override
      public void callback(int code, String message) {
        listener.onPayResult(code, message, payInfo);
      }
    })){
      return;
    }

    PayTask payTask = payTaskMap.get(payInfo);
    if(payTask == null) {
      payTask = new PayTask(activity, payInfo, listener);
      payTaskMap.put(payInfo, payTask);
    }

    if (!UserStorage.getFromFcm(activity) && !InitManager.getInstance().isInitOk()) {
      payTask.handPayCallBack(ErrorConstant.HYKB_CODE_SDK_NOT_INIT_OK,
              ErrorConstant.ERR_MSG_SDK_NOT_INIT_OK);
      return;
    }

    if (payInfo != null && TextUtils.isEmpty(payInfo.goodsName) && listener != null) {
      payTask.handPayCallBack(ErrorConstant.HYKB_CODE_PAY_GOOD_EMPTY,
              ErrorConstant.ERR_MSG_PAY_GOOD_EMPTY);
      return;
    }

    if (payInfo != null && payInfo.goodsName.length() > PayConstant.GOOD_NAME_MAX_LENGTH && listener != null) {
      payTask.handPayCallBack(ErrorConstant.HYKB_CODE_PAY_GOOD_LENGTH_OVER,
              ErrorConstant.ERR_MSG_PAY_GOOD_LENGTH_OVER);
      return;
    }

    if (payInfo != null && payInfo.money <= 0 && listener != null) {
      payTask.handPayCallBack(ErrorConstant.HYKB_CODE_PAY_MONEY_NEED_OVER_ZERO,
              ErrorConstant.ERR_MSG_PAY_MONEY_NEED_OVER_ZERO);
      return;
    }

    if (payInfo != null && payInfo.money > PayConstant.PAY_MAX_MONEY && listener != null) {
      payTask.handPayCallBack(ErrorConstant.HYKB_CODE_PAY_MONEY_NEED_LESS_5W,
              ErrorConstant.ERR_MSG_PAY_MONEY_NEED_LESS_5W);
      return;
    }

    if (payInfo != null && TextUtils.isEmpty(payInfo.cpOrderId) && listener != null) {
      //cp的订单号不能为空
      payTask.handPayCallBack(ErrorConstant.HYKB_CODE_PAY_CP_ORDER_EMPTY,
              ErrorConstant.ERR_MSG_PAY_CP_ORDER_EMPTY);
      return;
    }

    if (payInfo != null && payInfo.cpOrderId.length() > PayConstant.CP_ORDER_ID_MAX_LENGTH && listener != null) {
      //cp的订单号不能为空
      payTask.handPayCallBack(ErrorConstant.HYKB_CODE_PAY_CP_ORDER_LENGTH_OVER,
              ErrorConstant.ERR_MSG_PAY_CP_ORDER_LENGTH_OVER);
      return;
    }

    //判断用户信息是否来自防沉迷的登录
    if (UserStorage.getFromFcm(activity)) {
      //创建登录信息
      LoginManager.getInstance().createUserFrom(activity);
    }

    if (LoginManager.getInstance().getUser() != null) {
      payTask.handPayCallBack(ErrorConstant.HYKB_CODE_SDK_NOT_LOGIN,
              ErrorConstant.ERR_MSG_SDK_NOT_LOGIN);
      return;
    }

    if (payTask.isPaying()) {
      //已经在支付中了，不让重复调用
      return;
    }
    payTask.startPay();
  }


  /**
   * 对外接口，给游戏上报，发放商品的通知
   * @param cpOrderId
   * @param money
   * @param goodsName
   */
  public void reportNotifyGoods(String cpOrderId, String money, String goodsName) {
    payModel.notifyGoods(cpOrderId, money, goodsName);
  }

}
